package com.manger.common.tcp.server.handle;

import com.manger.common.utils.DataTools;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;

public class TcpServerHeartBeatHandler extends ChannelInboundHandlerAdapter {


    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {

        if (evt instanceof IdleStateEvent) {
            IdleState state = ((IdleStateEvent) evt).state();
            System.out.println("server: " + state.name());
            if(state == IdleState.WRITER_IDLE){
              //  TcpServer.getInstance().getClientChannel().writeAndFlush(new HeartEncode().encode(""));
            }else if(state == IdleState.READER_IDLE){
                DataTools.setTcpConnected(false);
            }
        }
        super.userEventTriggered(ctx, evt);
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        super.channelActive(ctx);
    }
}
